/**
 * creation: 7-mrt-2008
 */
package org.jfonia.connect.logic;

import org.jfonia.connect.BasicNode;
import org.jfonia.connect.Callable;
import org.jfonia.connect.Value;

/**
 * Implementation of Callable which behaves like an 'if'. 
 * 
 * @author wijnand.schepens@gmail.com
 *
 */
public class IfElse extends BasicNode
{
	protected Value<Boolean> condition;
	protected Callable thenC;		// mustn't be null
	protected Callable elseC;		// optional: null if not used 
	
	public IfElse(Value<Boolean> condition, Callable thenC, Callable elseC)
	{
		this.condition = condition;
		this.thenC = thenC;
		this.elseC = elseC;
	}
	
	public IfElse(Value<Boolean> condition, Callable thenC)
	{
		this(condition, thenC, null);
	}

	/**
	 * Calls then-Callable if condition-value evaluates to true, otherwise calls else-Callable
	 * if it is not null. This is used as parameter to call.
	 * 
	 * @param param
	 * @return true if condition is true and then-call returns true or if condition is false and
	 * else-Callable is present and returns true; otherwise false  
	 */
	public boolean callThis(Object param)
	{
		if (condition.get().booleanValue())
			return thenC.call(this);
		else if (elseC != null)
			return elseC.call(this);
		else
			return false;
	}
		
}
